Inside Solaris logo
The Cobb Group This article is reprinted from the September 1996 issue of  Inside Solaris, a monthly publication of The Cobb Group.


Working with archives

If you're just starting with tar, reading the man page gives you a good idea how it works and how you can use it. However, the man page doesn't describe some of the conventions that experienced system administrators use for their tar archives. In this article, we'll show you two important rules that you should use when you create archives with tar.

One archive, one directory

The most common mistake beginners make with tar is to create an inconvenient archive. Most tar archives, when you extract them, create a directory with the same name as the tar file, but without the .tar extension. All the files are placed in this directory. This is the expected behavior of a tar archive.

The most inconvenient type of tar archive is one that doesn't create the subdirectory and instead places all the files in your current directory. This can fill a neatly crafted directory tree with lots of undesired undergrowth.

For example, on many Solaris installations, the home directories for new users are often placed in a specific location, like /export/home. Thus, your /export/home directory may look something like this:

$ ls /export/home
marco   linda-r liz     alvin   linda-b bin
misc    progs   martha  jeff    august  amy

Suppose Liz decides to leave the company. You might save everything in a tar archive just in case someone needs a file that Liz created. If you're inexperienced with tar archives, you might create the tar archive like this:

$ cd /export/home/liz
$ tar cvf /archive/liz *
a letter.1 1K
a letter.2 4K
a letter.3 1K
a data/ 0K
a data/addresses 1K

Here, you're inside Liz's home directory, and you're saving all the files in it to the file /archive/liz. The problem comes into play when you need to reinstate Liz's directory. You can do so by typing

$ mkdir /export/home/liz
$ cd /export/home/liz
$ tar xvf liz
tar: blocksize = 18
x letter.1, 893 bytes, 2 tape blocks
x letter.2, 3584 bytes, 7 tape blocks
x letter.3, 219 bytes, 1 tape blocks
x data/, 0 bytes, 0 tape blocks
x data/addresses, 773 bytes, 3 tape blocks

However, experienced UNIX users expect the tar file to contain a directory, with files and subdirectories beneath it. An experienced UNIX system administrator who wanted to restore Liz's directory would use the following commands:

$ cd /export/home
$ tar xvf /archive/liz
tar: blocksize = 18
x letter.1, 893 bytes, 2 tape blocks
...    

At this point, the system administrator will start cursing, because the /export/home directory is now being loaded with files and subdirectories. You should create a tar archive that other people may possibly use, like this:

$ cd /export/home
$ tar cvf /archive/liz liz

This way, when you expand the archive in the expected way, tar will create a directory named liz to hold all the files. None of the files in the archive will mix with files in the current directory or in other directories. 

This is important to system administrators because you usually want to restore an archive only for a short period of time. Once you're finished with the data, you may want to remove the data from your disk to save space.

When an archive drops files in your current directory, removing them can be difficult if there are already files in the current directory. And the difficulty is compounded if there are multiple subdirectories. How will the system administrator know which files to remove? Luckily, if this happens, you can use a nifty trick to remove the files: See the sidebar "Removing an Inconvenient Archive" following this article.

Consistent naming

Another way you can avoid surprising be-havior is to give your tar file the same name as the directory that you're copying. Thus, your archive should be named liz if you're archiving a directory named liz. This way, other system administrators automatically know what directory name tar will create when it extracts the files from the archive.

Conclusion

Many people have to learn these conventions the hard way. But you don't have to. If you use these two simple rules for all your tar archives, you'll make life simpler - both for you and your successor.

 

[The Cobb Group Home Page]

Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.

Questions? Comments?